from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from .ws import PushServer
from .models import (
    DBSession,
    Base,
    )


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    
    config = Configurator(settings=settings)
    session_factory = UnencryptedCookieSessionFactoryConfig('fastpmsigner', timeout = 3600, cookie_name = 'kfsdgakdkfag')
    config.set_session_factory(session_factory)
    
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.add_route('gotoLogin', '/')
    config.add_route('uploadTry','upload')
    config.add_route('dynDownload','rpc/custombg')
    setupTema(config)
    setupSubscriptor(config)
    setupNoticia(config)
    setupLogin(config)
    config.scan()
    
    PushServer().start()
    
    return config.make_wsgi_app()

def setupSubscriptor(config):
    config.add_route('getNewId', 'rpc/intereses/nuevo')
    
    config.add_route('listTemaClient', 'rpc/intereses/listarCliente')
    config.add_route('listTemaSubscript', 'rpc/intereses/listarTodos')
    config.add_route('getInteres', 'rpc/intereses/listar')
    config.add_route('addInteres', 'rpc/intereses/agregar')
    config.add_route('delInteres', 'rpc/intereses/borrar')
    
def setupNoticia(config):
    config.add_route('listNoticia', 'rpc/noticia/listar')
    config.add_route('delNoticia', 'rpc/noticia/borrar')
    config.add_route('saveNoticia', 'rpc/noticia/guardar')
    
def setupTema(config):
    config.add_route('listTema', 'rpc/tema/listar')
    config.add_route('delTema', 'rpc/tema/borrar')
    config.add_route('saveTema', 'rpc/tema/guardar')

def setupLogin(config):
    config.add_route('listUsers', 'rpc/users')
    config.add_route('loginAttempt', 'rpc/login')
    config.add_route('logout', 'rpc/logout') 
    config.add_route('isLogged', 'rpc/isLogged')